package com.wenwei.sharebox.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.umeng.message.PushAgent;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.activity.AuthenticationActivity;
import com.wenwei.sharebox.activity.ForgetPwActivity;
import com.wenwei.sharebox.activity.GainIntegralActivity;
import com.wenwei.sharebox.activity.MessageBoxActivity;
import com.wenwei.sharebox.activity.RegisterActivity;
import com.wenwei.sharebox.activity.SysSettingActivity;
import com.wenwei.sharebox.activity.UserAppointmentActivity;
import com.wenwei.sharebox.activity.UserInfoActivity;
import com.wenwei.sharebox.activity.UserProvideActivity;
import com.wenwei.sharebox.activity.UserWalletActivity;
import com.wenwei.sharebox.base.BaseFragment;
import com.wenwei.sharebox.base.Constant;
import com.wenwei.sharebox.bean.UserLoginBean;
import com.wenwei.sharebox.utils.RogerSPUtils;
import com.wenwei.sharebox.utils.UserStringUtils;
import com.wenwei.sharebox.view.AuthenticationPopup;
import com.wenwei.sharebox.view.LoginPopup;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/7/6.
 */
public class PersonalFragment extends BaseFragment {
    @Bind(R.id.personal_scroll)
    ScrollView personalScroll;
    @Bind(R.id.m_content)
    LinearLayout mContentLl;


    private static final int REQUEST_CODE_REGISTER = 99;
    private static final int REQUEST_CODE_LOGIN = 88;
    private static final int REQUEST_CODE_FORGET = 77;
    @Bind(R.id.no_login_rl)
    RelativeLayout noLoginRl;
    @Bind(R.id.login_rl)
    RelativeLayout loginRl;
    @Bind(R.id.personal_user_head)
    CircleImageView personalUserHead;
    @Bind(R.id.personal_user_name)
    TextView personalUserName;

    private LoginPopup loginPopup;

    @Override
    public View initView(LayoutInflater inflater) {
        return LayoutInflater.from(mActivity).inflate(R.layout.fragment_personal, null);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        UserLoginBean ub = RogerSPUtils.getObject(mActivity, Constant.SP_USER_LOGIN_RESULT,UserLoginBean.class);
        if (ub != null) {
            //用户已登录
            setUserInfo();
        }

    }

    private boolean userAuthentication = false;
    private void setUserInfo(){
        UserLoginBean ub = RogerSPUtils.getObject(mActivity, Constant.SP_USER_LOGIN_RESULT,UserLoginBean.class);
        if(ub.getStatus() == 1){
            userAuthentication = false;
        }else {
            userAuthentication = true;
        }
        if(userAuthentication){
            new AuthenticationPopup(mActivity, mContentLl, new AuthenticationPopup.CallBack() {
                @Override
                public void onRightClick() {

                }

                @Override
                public void onLeftClick() {
                    goActivity(mActivity, AuthenticationActivity.class);
                }
            });
        }
        noLoginRl.setVisibility(View.GONE);
        loginRl.setVisibility(View.VISIBLE);
        Glide.with(mActivity).load(ub.getProfile().getAvator()).error(R.drawable.banner).into(personalUserHead);
        personalUserName.setText(ub.getUsername());
    }

    @Override
    protected void lazyload() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.personal_user_head, R.id.personal_login_tv, R.id.personal_register_tv, R.id.personal_wallet_ll, R.id.personal_integral_ll, R.id.personal_yu_yue_ll, R.id.personal_setting_ll, R.id.personal_gong_ll, R.id.message_box_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.personal_user_head:
                goActivity(mActivity, UserInfoActivity.class,null,66);
                break;
            case R.id.personal_login_tv:
                showLoginPopup();
                break;
            case R.id.personal_register_tv:
                goActivity(mActivity, RegisterActivity.class, null, REQUEST_CODE_REGISTER);
                break;
            case R.id.personal_wallet_ll:
                goActivity(mActivity, UserWalletActivity.class);
                break;
            case R.id.personal_integral_ll:
                goActivity(mActivity, GainIntegralActivity.class);
                break;
            case R.id.personal_yu_yue_ll:
                goActivity(mActivity, UserAppointmentActivity.class);
                break;
            case R.id.personal_setting_ll:
                goActivity(mActivity, SysSettingActivity.class);
                break;
            case R.id.personal_gong_ll:
                goActivity(mActivity, UserProvideActivity.class);
                break;
            case R.id.message_box_iv:
                goActivity(mActivity, MessageBoxActivity.class);
                break;
        }
    }

    private void showLoginPopup() {
        new LoginPopup(mActivity, mContentLl, new LoginPopup.CallBack() {
            @Override
            public void onForgetClick() {
                goActivity(mActivity, ForgetPwActivity.class);
            }

            @Override
            public void onLoginSuccess() {
                setUserInfo();
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case REQUEST_CODE_REGISTER:
                    showLoginPopup();
                    break;
                case REQUEST_CODE_LOGIN:
                    break;
                case REQUEST_CODE_FORGET:
                    break;
                case 66:
                    setUserInfo();
                    break;
            }
        }
    }
}
